<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="名称" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入名称" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['hotel_basic/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange" ref="tableRef">
                    <!-- <el-table-column type="selection" width="55" /> -->
                    <el-table-column label="逝者姓名" prop="name" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="逝者出生日期" prop="birth_date" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="逝者去世日期" prop="death_date" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="单人馆/双人馆" prop="is_double" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.is_double == 0 ? '单人馆' : '双人馆' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="宗教" prop="religion" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.religion ? religionList.find(item => item.value == row.religion)?.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="情感关系" prop="relationship" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.relationship ? relationshipList.find(item => item.id === row.relationship)?.title : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="国籍" prop="nationality" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.nationality ? nationalityList.find(item => item.value == row.nationality)?.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="职业" prop="career" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.career ? careerList.find(item => item.value == row.career)?.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="祖籍" prop="ancestral" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.ancestral ? ancestralList.find(item => item.value == row.ancestral)?.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="逝者照片" prop="avatar" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            <el-image :src="row.avatar" style="width: 50px; height: 50px;" min-width="150"/>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="祖籍" prop="ancestral" show-overflow-tooltip min-width="150"/> -->
                    <!-- <el-table-column label="墓地别名" prop="alias" show-overflow-tooltip min-width="150"/> -->


                    <el-table-column label="逝者姓名" prop="name2" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="逝者出生日期" prop="birth_date2" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="逝者去世日期" prop="death_date2" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="宗教" prop="religion2" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.religion2 ? religionList.find(item => item.value == row.religion2)?.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="情感关系" prop="relationship2" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            {{ row.relationship2 ? relationshipList.find(item => item.id === row.relationship2)?.title : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="国籍" prop="nationality2" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="职业" prop="career2" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="逝者照片" prop="avatar2" show-overflow-tooltip min-width="150">
                        <template #default="{ row }">
                            <el-image :src="row.avatar2" style="width: 50px; height: 50px;" min-width="150"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="祖籍" prop="ancestral2" show-overflow-tooltip min-width="150"/>
                    
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link >
                                <router-link :to="{ path: '/graveyardManage/graveyardEdit', query: { id: row.id } }" >编辑</router-link>
                            </el-button>
                            <el-button v-if="row.status == 1" v-perms="['hotel_basic/delete']" type="danger" link @click="handleDelete(row.id)" >删除</el-button>
                            <el-button v-if="row.status == 0" v-perms="['hotel_basic/recover']" type="primary" link @click="handleRecover(row.id)">恢复</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="墓地别名" prop="alias" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="简介" prop="content" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="墓碑祭词" prop="sacrificial_word" show-overflow-tooltip min-width="150"/>
                    <el-table-column label="署名" prop="sign_name" show-overflow-tooltip min-width="150"/>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="hotelBasicLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiGraveyardLists, apiGraveyardDelete, apiGraveyardAdd, apiGraveyardEdit, apiGraveyardDetail, apiGraveyardRecover } from '@/api/graveyardManage/list'
import { timeFormat } from '@/utils/util'
import { dictTypeLists, dictDataLists } from '@/api/setting/dict'
import { apiRelationLists } from '@/api/graveyardManage/emotionalRelationship'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { reactive, ref, shallowRef, computed, nextTick, onMounted, provide } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    title: '',
})
// 情感关系列表
const relationshipList = ref<any[]>([])
// 宗教列表
const religionList = ref<any[]>([])
// 祖籍列表
const ancestralList = ref<any[]>([])
// 国籍列表
const nationalityList = ref<any[]>([])
// 职业列表
const careerList = ref<any[]>([])
// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiGraveyardLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    // showEdit.value = true
    await nextTick()
    router.push({
        path: '/graveyardManage/graveyardEdit',
        query: {}
    })
    // editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    // editRef.value?.open('edit')
    // editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiGraveyardDelete({ id })
    getLists()
}

// 恢复
const handleRecover = async (id: number | any[]) => {
    await feedback.confirm('确定要恢复？')
    await apiGraveyardRecover({ id })
    getLists()
}
const getReligionList = async () => {
    const data = await dictTypeLists({type: 'dict_religion'})
    const religionData = await dictDataLists({type_id: data.lists[0].id})
    religionList.value = religionData.lists
}

const getAncestralList = async () => {
    const data = await dictTypeLists({type: 'dict_ancestral'})
    const ancestralData = await dictDataLists({type_id: data.lists[0].id})
    ancestralList.value = ancestralData.lists
}
const getCareerList = async () => {
    const data = await dictTypeLists({type: 'dict_career'})
    const careerData = await dictDataLists({type_id: data.lists[0].id})
    careerList.value = careerData.lists
}
const getNationalityList = async () => {
    const data = await dictTypeLists({type: 'dict_nationality'})
    const nationalityData = await dictDataLists({type_id: data.lists[0].id})
    nationalityList.value = nationalityData.lists
}
const getRelationshipList = async () => {
    const data = await apiRelationLists({page_no: 1, page_size: 9999})
    relationshipList.value = data.lists
}

// 监听路由变化
watch(
  () => route.fullPath,
  (newPath, oldPath) => {
    console.log("路由变化:", oldPath, "→", newPath);
    // 判断是否是从 edit 返回 index
    if (newPath === "/graveyardManage/list" && oldPath.includes("/graveyardEdit")) {
      getLists();
    }
  }
);

onMounted(() => {
    getReligionList()
    getAncestralList()
    getCareerList()
    getNationalityList()
    getRelationshipList()
    getLists()
})

</script>

